package com.guigu.dh.service.impl;

import com.guigu.dh.common.result.Result;
import com.guigu.dh.pojo.hosp.Hospital;
import com.guigu.dh.pojo.hosp.Schedule;
import com.guigu.dh.repository.ScheduleRepository;
import com.guigu.dh.service.HospitalService;
import com.guigu.dh.service.ScheduleService;
import com.guigu.dh.vo.hosp.BookingScheduleRuleVO;
import org.joda.time.DateTime;
import org.joda.time.DateTimeConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ScheduleServiceImpl implements ScheduleService {

    @Autowired
    private ScheduleRepository scheduleRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private HospitalService hospitalService;

    // 根据医院编号和科室编号查
    @Override
    public Map<String, Object> findSchedulePage(Integer currentPage, Integer limit, String hoscode, String depcode) {
        // 封装查询条件
        Criteria criteria = Criteria.where("hoscode").is(hoscode)
                .and("depcode").is(depcode);

        // 根据安排日期分组
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(criteria), // 匹配条件
                Aggregation.group("workDate") // 分组字段
                .first("workDate").as("workDate")
                // 统计号数量
                .count().as("docCount")
                .sum("reservedNumber").as("reservedNumber")
                .sum("availableNumber").as("availableNumber"),
                // 排序
                Aggregation.sort(Sort.Direction.DESC,"workDate"),
                // 分页
                Aggregation.skip((currentPage-1) * limit),
                Aggregation.limit(limit)
                );

        // 执行方法
        AggregationResults<BookingScheduleRuleVO> scheduleRuleVOS =
                mongoTemplate.aggregate(aggregation, Schedule.class, BookingScheduleRuleVO.class);

        // 得到结果
        List<BookingScheduleRuleVO> mappedResults = scheduleRuleVOS.getMappedResults();

        // 分组查询总记录数
        Aggregation newAggregationCount = Aggregation.newAggregation(
                Aggregation.match(criteria), // 匹配条件
                Aggregation.group("workDate") // 分组字段
                 );
        // 执行
        AggregationResults<BookingScheduleRuleVO> aggregationResults = mongoTemplate.
                aggregate(newAggregationCount, Schedule.class, BookingScheduleRuleVO.class);
        // 获取总记录数
        int total = aggregationResults.getMappedResults().size();

        //把日期对应星期获取
        for (BookingScheduleRuleVO bookingScheduleRuleVo : mappedResults) {
            Date workDate = bookingScheduleRuleVo.getWorkDate();
            String dayOfWeek = this.getDayOfWeek(new DateTime(workDate));
            bookingScheduleRuleVo.setDayOfWeek(dayOfWeek);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("content",mappedResults);

        // 获取医院信息
        Hospital hospital = hospitalService.getHospitalInfo(hoscode);
        hospital.setLogoData("");
        // 加入map中
        map.put("hospital",hospital);
        return map;
    }

    @Override
    public Result showScheduleByWorkDate(String hoscode, String depcode, String workDate) {
        List<Schedule> schedule =
                scheduleRepository.getScheduleByHoscodeAndDepcodeAndWorkDate(hoscode, depcode, new DateTime(workDate).toDate());
        return Result.ok(schedule);
    }

    /**
     * 根据日期获取周几数据
     *
     * @param dateTime
     * @return
     */
    private String getDayOfWeek(DateTime dateTime) {
        String dayOfWeek = "";
        switch (dateTime.getDayOfWeek()) {
            case DateTimeConstants.SUNDAY:
                dayOfWeek = "周日";
                break;
            case DateTimeConstants.MONDAY:
                dayOfWeek = "周一";
                break;
            case DateTimeConstants.TUESDAY:
                dayOfWeek = "周二";
                break;
            case DateTimeConstants.WEDNESDAY:
                dayOfWeek = "周三";
                break;
            case DateTimeConstants.THURSDAY:
                dayOfWeek = "周四";
                break;
            case DateTimeConstants.FRIDAY:
                dayOfWeek = "周五";
                break;
            case DateTimeConstants.SATURDAY:
                dayOfWeek = "周六";
            default:
                break;
        }
        return dayOfWeek;
    }
}
